<!--
  -    Copyright (c) 2018-2025, cloud All rights reserved.
  -
  - Redistribution and use in source and binary forms, with or without
  - modification, are permitted provided that the following conditions are met:
  -
  - Redistributions of source code must retain the above copyright notice,
  - this list of conditions and the following disclaimer.
  - Redistributions in binary form must reproduce the above copyright
  - notice, this list of conditions and the following disclaimer in the
  - documentation and/or other materials provided with the distribution.
  - Neither the name of the pig4cloud.com developer nor the names of its
  - contributors may be used to endorse or promote products derived from
  - this software without specific prior written permission.
  - Author: cloud
  -->
<template>
  <BasicContainer>
    <ElRow :span="24">
      <ElCol :xs="24" :sm="24" :md="5" class="user__tree">
        <FyTree :option="treeOption" :data="treeData" @node-click="nodeClick">
          <template #default="{ node, data }">
            <span class="el-tree-node__label">
              <ElTooltip v-if="data.isLock" class="item" effect="dark" content="无数据权限" placement="right-start">
                <span>{{ node.label }} <i class="el-icon-lock"></i></span>
              </ElTooltip>
              <span v-if="!data.isLock">{{ node.label }}</span>
            </span>
          </template>
        </FyTree>
      </ElCol>
      <ElCol :xs="24" :sm="24" :md="19" class="regional__main">
        <FyCurd
          ref="crud"
          v-model="form"
          :page.sync="page"
          right-search
          :data="tableData"
          :permission="permissionList"
          :table-loading="tableLoading"
          :option="tableOption"
          :before-open="handleOpenBefore"
          @on-load="getList"
          @search-change="searchChange"
          @refresh-change="refreshChange"
          @size-change="sizeChange"
          @current-change="currentChange"
          @row-update="handleUpdate"
          @row-save="handleSave"
          @row-del="handleDel">
          <template #menuLeft>
            <ElButton
              class="filter-item"
              plain
              type="primary"
              size="small"
              icon="el-icon-upload"
              @click="$refs.excelUpload.show()">
              导入
            </ElButton>
            <ElButton
              v-if="permissions.regional_fyroomregional_export"
              class="filter-item"
              plain
              type="primary"
              size="small"
              icon="el-icon-download"
              @click="exportExcel">
              导出
            </ElButton>
          </template>
          <!-- 附件上传组件{ 需要传入uniqueCode/model/classify} -->
          <template #uploadPageForm>
            <UploadPage ref="UploadPage" :unique-code="uniqueCode" model="regional" classify="regional" />
          </template>
        </FyCurd>
      </ElCol>
      <!--excel 模板导入 -->
      <ExcelUpload
        ref="excelUpload"
        title="区域信息导入"
        :url="`${spaceService}fyroomregional/import`"
        :temp-url="`${spaceService}fyroomregional/local/regionalTemp.xlsx`"
        temp-name="区域导入模板.xlsx"
        @refreshDataList="refreshChange" />
    </ElRow>
  </BasicContainer>
</template>

<script>
  import { addObj, delObj, fetchList, putObj, regionalFetchTree } from '@/api/space/fyroomregional'
  import { tableOption } from '@/const/crud/space/fyroomregional'
  import { getUUID } from '@/api/admin/uuid'

  import { services } from '@/config'
  import { mapState } from 'vuex'

  export default {
    name: 'Fyroomregional',

    data() {
      return {
        spaceService: services.spaceService,
        searchForm: {},
        tableData: [],
        treeOption: {
          nodeKey: 'id',
          addBtn: false,
          menu: false,
          props: {
            label: 'name',
            value: 'id'
          }
        },
        treeData: [],
        page: {
          total: 0, // 总页数
          currentPage: 1, // 当前页数
          pageSize: 20 // 每页显示多少条
        },
        tableLoading: false,
        tableOption,
        form: {},
        uniqueCode: '',
        showUpload: false
      }
    },
    computed: {
      ...mapState('user', ['permissions']),
      permissionList() {
        return {
          addBtn: this.$isEmpty(this.permissions.regional_fyroomregional_add, false),
          delBtn: this.$isEmpty(this.permissions.regional_fyroomregional_del, false),
          editBtn: this.$isEmpty(this.permissions.regional_fyroomregional_edit, false)
        }
      }
    },
    created() {
      this.regional_fyroomregional_add = this.permissions.regional_fyroomregional_add
      this.regional_fyroomregional_edit = this.permissions.regional_fyroomregional_edit
      this.regional_fyroomregional_del = this.permissions.regional_fyroomregional_del
      this.init()
    },
    methods: {
      async init() {
        const response = await regionalFetchTree()
        this.treeData = response.data.data
      },
      nodeClick(data) {
        this.page.page = 1
        this.getList(this.page, { parentId: data.id })
      },
      // 列表查询
      getList(page, params) {
        this.tableLoading = true
        fetchList(
          Object.assign(
            {
              current: page.currentPage,
              size: page.pageSize
            },
            params,
            this.searchForm
          )
        )
          .then(response => {
            this.tableData = response.data.data.records
            this.page.total = response.data.data.total
            this.tableLoading = false
          })
          .catch(() => {
            this.tableLoading = false
          })
      },
      // 删除
      async handleDel(row, index) {
        try {
          await this.$confirm(`是否确认删除ID为${row.id}`, '提示', { type: 'warning' })
          await delObj(row.id)
          this.$message.success('删除成功')
          await this.getList(this.page)
        } catch (e) {}
      },
      // 更新
      handleUpdate(row, index, done, loading) {
        putObj(row)
          .then(data => {
            this.$message.success('修改成功')
            done()
            this.getList(this.page)
          })
          .catch(() => {
            loading()
          })
      },
      // 保存
      handleSave(row, done, loading) {
        if (row.ordernumber === 'undefined') {
          row.ordernumber = ''
        }
        addObj(row)
          .then(data => {
            this.$message.success('添加成功')
            done()
            this.getList(this.page)
          })
          .catch(() => {
            loading()
          })
      },
      // 每页条数改变事件
      sizeChange(pageSize) {
        this.page.pageSize = pageSize
      },
      // 当前页发生改变事件
      currentChange(current) {
        this.page.currentPage = current
      },
      // 查询事件
      searchChange(form, done) {
        this.searchForm = form
        this.page.currentPage = 1
        this.getList(this.page, form)
        done()
      },
      handleOpenBefore(show, type) {
        this.showUpload = true
        // 添加附件参数
        if (type === 'edit') {
          tableOption.column[33].data = { uniqueCode: this.form.unid, classify: 'regional', model: 'regional' }
        } else {
          getUUID().then(response => {
            this.uniqueCode = response.data.data
            this.form.unid = response.data.data
          })
        }
        window.boxType = type
        show()
      },
      // 刷新事件
      refreshChange() {
        this.getList(this.page)
      },
      //  导出excel
      exportExcel() {
        this.$downBlobFile(`${services.spaceService}fyroomregional/export`, this.searchForm, 'fyroomregional.xlsx')
      }
    }
  }
</script>
